package com.csdj.article.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csdj.article.common.WebResultMsgCode;
import com.csdj.article.entity.Article;
import com.csdj.article.entity.WebResult;
import com.csdj.article.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 文章 前端控制器
 * </p>
 *
 * @author Hh
 * @since 2022-03-03
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private IArticleService articleService;

    /**
     *  查看文章
     * @return WebResult
     */
    @GetMapping("article")
    public WebResult getArticle(){
        List<Article> list=articleService.list();
        if(list!=null){
            return new WebResult(WebResultMsgCode.SUCCESS,list);
        }else{
            return new WebResult(WebResultMsgCode.USER_NO_LOGIN);
        }
    }

    /**
     * 发布添加文章
     * @param article
     * @return
     */
    public WebResult publishArticle(@RequestBody Article article){
        //TODO 验证一下前端传的值是否满足新增所需必填参数
        if(article!=null){
            if(articleService.save(article)){
                //TODO 新增成功之后维护一下用户表里的文章数量字段(好像没有这个字段)
                return new WebResult(WebResultMsgCode.SUCCESS,article);
            }
        }
        return new WebResult(WebResultMsgCode.USER_NO_LOGIN);
    }
    @GetMapping("pageArticle")
    public WebResult page(){
        Page<Article> page=new Page<>(1,5);
        IPage<Article> iPage=articleService.page(page);
        if(iPage!=null){
            return new WebResult(WebResultMsgCode.SUCCESS,iPage);
        }else {
            return new WebResult(WebResultMsgCode.USER_NO_LOGIN);
        }
    }
}
